
/**
 * Date:2019年10月29日上午11:21:59
 * Copyright (c) 2019, www.bwbroad.com All Rights Reserved.
 *
*/

package com.app.common.core;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionAttribute;
import org.springframework.transaction.interceptor.TransactionInterceptor;

/**
 * Description: 添加按方法名称拦截事务 <br/>
 * 在 ProxyTransactionManagementConfiguration 类中已经支持了注解事务 Date: 2019年10月29日
 * 上午11:21:59 <br/>
 * 
 * @author xuejianxin
 */
@Configuration
public class TransactionAdviceConfig {
	@Autowired
	@Qualifier("myTransactionManager")
	private TransactionManager transactionManager;//事物管理器

	@Bean("myTransactionInterceptor")
	public TransactionInterceptor myTransactionInterceptor() {
		NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
		// 只读事务，不做更新操作
		RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
		readOnlyTx.setReadOnly(true);
		//PROPAGATION_SUPPORTS 如果当前有事务则加入，如果没有则不用事务
		//readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
		//支持当前事务，如果当前有事务， 那么加入事务， 如果当前没有事务则新建一个(默认情况)
		readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		// 当前存在事务就使用当前事务，当前不存在事务就创建一个新的事务
		// RuleBasedTransactionAttribute requiredTx = new
		// RuleBasedTransactionAttribute();
		// requiredTx.setRollbackRules(
		// Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
		// requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		//支持当前事务，如果当前有事务， 那么加入事务， 如果当前没有事务则新建一个(默认情况)
		RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute(
				TransactionDefinition.PROPAGATION_REQUIRED,
				Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
		// requiredTx.setTimeout(5);//设置超时
		Map<String, TransactionAttribute> txMap = new HashMap<>();
		txMap.put("*", readOnlyTx);// 只读事务
		txMap.put("save*", requiredTx);
		txMap.put("insert*", requiredTx);
		txMap.put("update*", requiredTx);
		txMap.put("delete*", requiredTx);
		source.setNameMap(txMap);
		//org.springframework.jdbc.support.JdbcTransactionManager
		System.out.println(transactionManager.getClass().getName());
		
		return new TransactionInterceptor(transactionManager, source);
	}

	@Bean
	public BeanNameAutoProxyCreator transactionAutoProxy() {
		BeanNameAutoProxyCreator transactionAutoProxy = new BeanNameAutoProxyCreator();
		transactionAutoProxy.setProxyTargetClass(true);
		// 事物类以Service 或 ServiceImpl 结尾
		transactionAutoProxy.setBeanNames("*Service", "*ServiceImpl");
		transactionAutoProxy.setInterceptorNames("myTransactionInterceptor");
		return transactionAutoProxy;
	}

}
